<?php

// +----------------------------------------------------------------------
// | api基类控制器
// +----------------------------------------------------------------------
// | @author fancsen
// +----------------------------------------------------------------------

/**
 * api基类控制器
 * @author fancsen
 */


namespace app\api\controller;

use think\Controller;
use app\api\service\Token as TokenService;
use app\common\lib\exception\ApiException;

class BaseController extends Controller
{
    /**
     * 前置方法：权限控制-user&super
     */
    protected function checkPrimaryScope()
    {
        TokenService::needPrimaryScope();
    }

    /**
     * 前置方法：权限控制-user
     */
    protected function checkExclusiveScope()
    {
        TokenService::needExclusiveScope();
    }


    /**
     * 前置操作-覆盖父类方法
     * @access protected
     * @param  string $method  前置操作方法名
     * @param  array  $options 调用参数 ['only'=>[...]] 或者 ['except'=>[...]]
     * @return void
     */
    protected function beforeAction($method, $options = [])
    {
        if (isset($options['only'])) {
            if (is_string($options['only'])) {
                $options['only'] = explode(',', $options['only']);
            }

            if (!in_array($this->request->action(true), $options['only'])) {
                return;
            }
        } elseif (isset($options['except'])) {
            if (is_string($options['except'])) {
                $options['except'] = explode(',', $options['except']);
            }

            if (in_array($this->request->action(true), $options['except'])) {
                return;
            }
        }

        call_user_func([$this, $method]);
    }
}